package me.sdevil507.biz.fruit.controller;

import com.alibaba.druid.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.fruit.dto.FruitDTO;
import me.sdevil507.biz.fruit.dto.FruitOrgRelDTO;
import me.sdevil507.biz.fruit.dto.FruitParamsDTO;
import me.sdevil507.biz.fruit.dto.ResultListFruitDTO;
import me.sdevil507.biz.fruit.mapper.FruitMapper;
import me.sdevil507.biz.fruit.po.FruitFeedBackPO;
import me.sdevil507.biz.fruit.po.FruitOrgRelPo;
import me.sdevil507.biz.fruit.po.FruitPO;
import me.sdevil507.biz.fruit.po.ResultPO;
import me.sdevil507.biz.fruit.repository.FruitRepository;
import me.sdevil507.biz.fruit.service.FruitImportExportService;
import me.sdevil507.biz.fruit.service.FruitOrgRelService;
import me.sdevil507.biz.fruit.service.FruitService;
import me.sdevil507.org.po.OrgUserPo;
import me.sdevil507.org.service.OrgUserService;
import me.sdevil507.org.service.TempExpertService;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.shiro.helper.OrgAccountHelper;
import me.sdevil507.supports.status.ApiStatusCode;
import me.sdevil507.supports.status.ComConstant;
import me.sdevil507.systemAssist.Constant.SystemAssistConstant;
import me.sdevil507.systemAssist.systemLog.annotation.OperLog;
import me.sdevil507.systemAssist.systemLog.service.SystemErrorLogService;
import me.sdevil507.systemAssist.systemLog.service.SystemOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @program: tips_back_end
 * @description: 成果展示类
 **/
@RestController
@RequestMapping("/api/wechat/fruit")
@Slf4j
public class FruitWechatController {

    @Autowired
    private FruitService fruitService;

    @Autowired
    private OrgUserService orgUserService;

    /**
     * 设置id
     * @param dto
     */
    public void setBusinessId(FruitDTO dto) throws Exception {
        //获取userId
        OrgUserPo orgUserPo = orgUserService.readOneByPhoneNumber(dto.getLoginPhoneNumber());
        if(orgUserPo != null){
            dto.setOrgUserId(orgUserPo.getId());
            dto.setUserId(orgUserPo.getId());
        }else{
            log.error("当前手机号账户不存在");
        }
    }

    /**
     * 新增成果
     *
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @OperLog(operTerminal = SystemAssistConstant.systemAssistTerminal.WECHAT, operClassify = SystemAssistConstant.systemAssistClassify.FRUIT, operModule = SystemAssistConstant.systemAssistModule.FRUIT_ADD, operDesc = "")
    @PostMapping("/create")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO create(@RequestBody FruitDTO dto) {
        ApiResultDTO apiResultDTO;
        try {
            this.setBusinessId(dto);
            fruitService.expertCreate(dto);
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            //事务回滚
            log.error("添加成果失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            apiResultDTO = ApiResultGenerator.create("6001", "新增成果失败");
        }
        return apiResultDTO;
    }
}
